package com.hongxun.project.assets.pPole.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hongxun.common.utils.text.Convert;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.pPole.domain.PPole;
import com.hongxun.project.assets.pPole.service.IPPoleService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 低压杆塔采集 信息操作处理
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/pPole")
public class PPoleController extends BaseController
{
    private String prefix = "assets/pPole";
	
	@Autowired
	private IPPoleService pPoleService;
	@Autowired
	private IPPointService pPointService;
	
	@RequiresPermissions("assets:pPole:view")
	@GetMapping()
	public String pPole()
	{
	    return prefix + "/pPole";
	}

	/*@GetMapping("/task_pPole/{taskId}")
	public String task_pPole(@PathVariable("taskId") String taskId , ModelMap modelMap)
	{
		modelMap.put("taskId",taskId);
	    return prefix + "/task-pPole";
	}*/
	
	/**
	 * 查询低压杆塔采集列表
	 */
	@RequiresPermissions("assets:pPole:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PPole pPole)
	{
		startPage();
        List<PPole> list = pPoleService.selectPPoleList(pPole);
		return getDataTable(list);
	}

	
	/**
	 * 导出低压杆塔采集列表
	 */
	@RequiresPermissions("assets:pPole:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PPole pPole)
    {
    	List<PPole> list = pPoleService.selectPPoleList(pPole);
        ExcelUtil<PPole> util = new ExcelUtil<PPole>(PPole.class);
        return util.exportExcel(list, "pPole");
    }
	
	/**
	 * 新增低压杆塔采集
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存低压杆塔采集
	 */
	@RequiresPermissions("assets:pPole:add")
	@Log(title = "低压杆塔采集", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PPole pPole)
	{
		pPole.setDatastate("新增");
		return toAjax(pPoleService.insertPPole(pPole));
	}

	/**
	 * 修改低压杆塔采集
	 */
	@GetMapping("/edit/{id}/{taskId}")
	public String edit(@PathVariable("id") String id,@PathVariable("taskId") String taskId, ModelMap mmap)
	{
		PPole pPole1 = new PPole();
		pPole1.setPId(id);
		pPole1.setDatastate("删除");
		List<PPole> pPoleList = pPoleService.selectPPoleList(pPole1);
		PPole pPole = new PPole();
		if(pPoleList.size() == 0){
			PPoint pPoint = pPointService.selectPPointByIdForState(id,taskId);
			pPole1.setPId( pPoint.getPoleId());
			pPoleList = pPoleService.selectPPoleList(pPole1);
			pPole = pPoleService.selectPPoleByIdForState(pPoleList.get(0).getId(),taskId);
		}else{
			pPole = pPoleService.selectPPoleByIdForState(pPoleList.get(0).getId(),taskId);
		}
		mmap.put("pPole", pPole);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存低压杆塔采集
	 */
	@RequiresPermissions("assets:pPole:edit")
	@Log(title = "低压杆塔采集", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PPole pPole)
	{
		// 点位信息
//		PPoint pPoint = pPointService.selectPPointByIdForState(pPole.getPId(),pPole.getTaskId());
		pPole.setUpdateUser(Integer.parseInt(getUserId().toString()));
		pPole.setUpdateDate(new Date());
		pPole.setDatastate("修改");
		return toAjax(pPoleService.updatePPole(pPole));
	}
	
	/**
	 * 删除低压杆塔采集
	 */
	@RequiresPermissions("assets:pPole:remove")
	@Log(title = "低压杆塔采集", businessType = BusinessType.DELETE)
	@PostMapping( "/remove2Id")
	@ResponseBody
	public AjaxResult remove2Id(String ids , String taskIds)
	{
		String[] str = Convert.toStrArray(ids);
		int i = 0;
		for (String s : str) {
			PPole pPole = new PPole();
			pPole.setId(s);
			pPole.setTaskId(taskIds);
			pPole.setDatastate("删除");
			i = pPoleService.updatePPole(pPole);
		}
		return toAjax(i);
//		return toAjax(pPoleService.deletePPoleByIdsAndTaskIds(ids , taskIds));
	}


	/**
	 * 删除低压杆塔采集
	 */
	@RequiresPermissions("assets:pPole:remove")
	@Log(title = "低压杆塔采集", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{

		return toAjax(pPoleService.deletePPoleByIds(ids));
	}
	
}
